
package com.android.generator.callback;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

public class CallbackDispathchGenerator {

    /**
     * private class AutopCallbackHandler extends CallbackHandler<Callback>{
     * public void dispatchOnAutopStarted() { for (Callback callback :
     * getCallbacks()) { callback.onAutopStarted(); } } public void
     * dispatchOnAutopStarted(int result) { for (Callback callback :
     * getCallbacks()) { callback.onAutopCompleted(result); } } }
     */

    private static final Class<?> TARGET = OnScrollListener.class;

    public static void main(String[] args) {
        String className = TARGET.getSimpleName() + "Handler";

        //New instance.
        String instanceName = " m" + className;
        System.out.println("public " + className + instanceName + " = new " + className + "();");

        //Register method
        System.out.println("public void registerCallback(" + TARGET.getSimpleName() + " callback){");
        System.out.println(instanceName + ".registerCallback(callback);");
        System.out.println("}");

        //Unregister method
        //Register method
        System.out.println("public void unRegisterCallback(" + TARGET.getSimpleName() + " callback){");
        System.out.println(instanceName + ".unRegisterCallback(callback);");
        System.out.println("}");

        //Callback handler.
        System.out.println("public class " + className + " extends CallbackHandler<" + TARGET.getSimpleName() + ">{");
        Method[] declaredMethods = TARGET.getDeclaredMethods();
        for (Method method : declaredMethods) {
            String returnType = method.getReturnType().getSimpleName();
            String methodName = method.getName();
            String methodFirstWord = methodName.substring(0, 1);
            String methodLastAllWords = methodName.substring(1, methodName.length());
            String newWorkName = methodFirstWord.toUpperCase() + methodLastAllWords;
            System.out.print("    public " + returnType + " " + "dispathch" + newWorkName + "(");
            Parameter[] parameters = method.getParameters();
            boolean hasParamter = (parameters.length != 0);
            if (hasParamter) {
                int length = parameters.length;
                for (int i = 0; i < length; i++) {
                    Parameter parameter = parameters[i];
                    System.out.print(parameter.getType().getSimpleName() + " " + parameter.getName()
                            + (i == (length - 1) ? "" : ","));
                }
                System.out.println("){");
                System.out.println("for (" + TARGET.getSimpleName() + " callback : getCallbacks()) {");
                System.out.print("callback." + method.getName() + "(");
                for (int i = 0; i < length; i++) {
                    Parameter parameter = parameters[i];
                    System.out.print(parameter.getName() + (i == (length - 1) ? "" : ","));
                }
                System.out.println(");");
                System.out.println("}");
            } else {
                System.out.println("){");
                System.out.println("for (" + TARGET.getSimpleName() + " callback : getCallbacks()) {");
                System.out.println("callback." + method.getName() + "();");
                System.out.println("}");
            }
            System.out.println("}");
        }
        System.out.println();
        System.out.println("}");
    }

}
